###############################################################################
# ifx2f - A Fortran library for Informix database clients
# See source code and Licensing information at http://code.google.com/p/ifx2f
#
# ifx2f/Makefile
###############################################################################

LIBNAME:=libifx2f
ifndef INFORMIXDIR
#INFORMIXDIR=/opt/informix
INFORMIXDIR=/usr/local/informix
endif
INFORMIX_CONF:=$$( grep ^INFORMIX_CONF config/$(LIBNAME).conf | cut -d'=' -f2- )
INFORMIXDIR:=$$(grep ^INFORMIXDIR config/informix.conf | cut -d'=' -f2- )
#INFORMIXSERVER:=$$(grep ^INFORMIXSERVER config/informix.conf | cut -d'=' -f2- )
#INFORMIXSQLHOSTS:=$$(grep ^INFORMIXSQLHOSTS config/informix.conf | cut -d'=' -f2- )

CPP             = g++
DEBUG           = -g
#DEBUG           = -DNDEBUG
CPPOPTIONS      = -Wall
CFORTRANDEFS    = -Df2cFortran -I/usr/include
COMPILE_DEFINES = -DLINUX -DIT_DO_NOT_SIMULATE_BOOL -DIT_HAS_DISTINCT_LONG_DOUBLE -DIT_COMPILER_HAS_LONG_LONG -DIT_DLLIB -DMITRACE_OFF
LINK_DEFINES    = $(COMPILE_DEFINES)
DEFINES         = $(COMPILE_DEFINES)
COMPILEROPTS    = -fPIC -fsigned-char
INCLUDEPATH     = -I$(INFORMIXDIR)/incl/c++ -I$(INFORMIXDIR)/incl/dmi -I$(INFORMIXDIR)/incl/esql -I$(INFORMIXDIR)/incl
LIBRARYPATH     = -L$(INFORMIXDIR)/lib/c++ -L$(INFORMIXDIR)/lib/dmi -L$(INFORMIXDIR)/lib/esql -L$(INFORMIXDIR)/lib
LIBRARIES       = -lifc++ -lifdmi -lifsql -lifasf -lifgen -lifos -lifgls -lifglx $(INFORMIXDIR)/lib/esql/checkapi.o
#LIBRARIES       = `$(INFORMIXDIR)/bin/esql -libs`
LDLIBRARYPATH   = -Wl,-rpath,$(INFORMIXDIR)/lib/c++ -Wl,-rpath,$(INFORMIXDIR)/lib/dmi -Wl,-rpath,$(INFORMIXDIR)/lib/esql -Wl,-rpath,$(INFORMIXDIR)/lib
SYSLIBS         = -lm -ldl -lcrypt -lnsl
#LIBCONFDEFAULTS        =-DINFORMIXDIR=\"$(INFORMIXDIR)\" -DINFORMIXSERVER=\"$(INFORMIXSERVER)\" -DINFORMIXSQLHOSTS=\"$(INFORMIXSQLHOSTS)\" -DINFORMIX_CONF=\"$(INFORMIX_CONF)\"
LIBCONFDEFAULTS =-DINFORMIX_CONF=\"$(INFORMIX_CONF)\"

#BASETYPE=64
BASETYPE=
prefix=/usr/local
exec_prefix=${prefix}
bindir=${exec_prefix}/bin
libdir=${exec_prefix}/lib${BASETYPE}
incdir=${exec_prefix}/include
docdir=${exec_prefix}/share/doc/${LIBNAME}
sharedir=${exec_prefix}/share/${LIBNAME}
INSTALL=/usr/bin/install -c

SRCDIR=src
INCDIR=include
LIBDIR=lib
DOCDIR=doc
VPATH=$(SRCDIR):$(INCDIR)
vpath %.h $(INCDIR)
vpath %.cpp $(SRCDIR)
vpath %.o $(SRCDIR)

#ifndef DESTDIR
#DESTDIR=$(PWD)/debian/libifx2f/
#endif

.PHONY: lib install examples distclean clean all
.PRECIOUS: %.o
.SUFFIXES: .cpp .h .a .o .exe

OBJECTS=$(SRCDIR)/$(LIBNAME).o

all: lib

lib: $(LIBNAME).so

$(LIBNAME).so: $(OBJECTS)
	$(CPP) $(DEBUG) $(CPPOPTIONS) $(DEFINES) $(COMPILEROPTS) $(INCLUDEPATH) \
               -shared -o $@ $< $(LIBRARYPATH) $(LIBRARIES) $(SYSLIBS) $(LDLIBRARYPATH)

%.cpp: Makefile %.h
%.o: %.cpp
	$(CPP) $(DEBUG) $(CPPOPTIONS) $(DEFINES) $(COMPILEROPTS) $(CFORTRANDEFS) \
               $(INCLUDEPATH) -I$(INCDIR) $(LIBCONFDEFAULTS) -c $< -o $@

install: Makefile $(LIBNAME).so

clean-o:
	rm -f $(SRCDIR)/*.o

clean: clean-o

clear: clean
	rm -f $(LIBNAME).so build-stamp configure-stamp

doc: doc/Makefile
	make -C doc

examples: examples/Makefile
	make -C examples

distclean: clean
	rm -f $(SRCDIR)/*.o
	rm -f $(LIBNAME).so
	make -C doc $@
	make -C examples $@

tarball: clean
	tar zcvf $(LIBNAME).`date +%Y%m%d%H%M%S`.tar.gz Makefile \
            config/*.conf src/*.cpp include/*.h \
            examples/Makefile examples/*.f90 \
            doc/README* \
            debian/README* debian/changelog debian/rules debian/control

install: Makefile $(LIBNAME).so config/sqlhosts config/$(LIBNAME).conf
	if [[ "$(DESTDIR)" != "" ]] ; then \
          for d in $(bindir) $(libdir) $(incdir) $(docdir) $(sharedir) ; do $(INSTALL) -d $(DESTDIR)$$d ; done ; \
          $(INSTALL) -m 0755 $(LIBNAME).so $(DESTDIR)$(libdir) ; \
          $(INSTALL) -m 0644 config/sqlhosts $(DESTDIR)$(sharedir) ; \
          $(INSTALL) -m 0644 config/informix.conf $(DESTDIR)$(sharedir) ; \
          $(INSTALL) -m 0644 config/$(LIBNAME).conf $(DESTDIR)$(sharedir) ; \
        else \
          for n in bin include lib/gnu share/$(LIBNAME) share/informix ; do $(INSTALL) -d $(HOME)/$$n ; done ; \
          $(INSTALL) -m 0755 $(LIBNAME).so $(HOME)/lib/gnu/$(LIBNAME).so ; \
          $(INSTALL) -m 0644 config/sqlhosts $(INFORMIXSQLHOSTS) ; \
          $(INSTALL) -m 0644 config/$(LIBNAME).conf $(HOME)/share/$(LIBNAME)/$(LIBNAME).conf ; \
        fi

